package com.szc.demo01.timer;

import com.szc.demo01.dao.AttenceMapper;
import com.szc.demo01.dao.EmpMapper;
import com.szc.demo01.pojo.Attence;
import com.szc.demo01.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class AttenceTimer {
    @Autowired
    private AttenceMapper attenceMapper;

    @Autowired(required = false)
    private EmpMapper empMapper;

    @Scheduled(cron = "10 50 10 * * ?")
    public void updateAttenceInNextDay(){
        List<Emp> empList = empMapper.getEmpList();
        Date date=new Date(System.currentTimeMillis());
        List<Attence>attenceList=new ArrayList<>();
        for(Emp emp:empList){
            Attence attence=new Attence();
            attence.setEId(emp.getId());
            attence.setWorkDate(date);
            attence.setIsLeave(0);
            attence.setIsLater(0);
            attenceList.add(attence);
        }
        int i = attenceMapper.batchInsert(attenceList);
    }

    @Scheduled(cron = "0 51 10 * * ?")
    public void updateAttenceForLeave(){
        Date date=new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        List<Attence> listForBossByDay = attenceMapper.getListForBossByDay(format);
        List<Attence>updateAttenceList=new ArrayList<>();
        for(Attence attence:listForBossByDay){
            //如果上班没有打卡，下班没打卡，算请假
            if(attence.getWorkStart()==null&&attence.getWorkTime()==0){
                updateAttenceList.add(attence);
            }
        }
        int i = attenceMapper.batchUpdate(updateAttenceList,format);
    }
}
